
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using System.Collections.Generic;
using System.Web.UI;

using Swaf.Container;
using Swaf.Gui;
using Swaf.Gui.AspNet2;

[assembly: WebResource("ScsAspNet.Syrinx.Gui.AspNet.Ajax.script.BaseLibrary.js", "application/x-javascript")]

namespace Syrinx.Gui.AspNet.Ajax
{
	public class NitobiBasePage : Swaf.Gui.AspNet2.BasePage, INitobiPage
	{
		protected string m_ajaxHandler = "/AjaxRequestHandler.aspx";
		protected bool m_usingAjax = true;
		protected bool m_first = true;
		protected bool m_checkForSslLinks = false;
		protected string m_hostUrl = null;
		protected bool m_inSslNow = false;

		protected override void OnPreInit(EventArgs e)
		{
			string masterPage = MasterPageFile;
			MasterPageFile = masterPage;
			base.OnPreInit(e);

			if (((Swaf.BizRule.IRuleManagerEx)Swaf.Application.globalApp.ruleMgr).getRuleInfo("isSecurePage", false) != null)
			{			
				m_checkForSslLinks = true;
				Uri first = (Uri)App.sessionData.get("firstUrl");
				m_hostUrl = first.Host + ":443";
				if (Request.Url.Scheme.CompareTo(Uri.UriSchemeHttps) == 0)
					m_inSslNow = true;
			}
		}

		protected override void OnLoad(EventArgs e)
		{
			m_first = false;
			Response.Buffer = true;
			if (App.sessionData["UseAjax"] is bool)
				m_usingAjax = (bool)App.sessionData["UseAjax"];
			if (m_usingAjax)
			{
				string scriptUrl = ClientScript.GetWebResourceUrl(typeof(NitobiBasePage), "ScsAspNet.Syrinx.Gui.AspNet.Ajax.script.BaseLibrary.js");
				ClientScript.RegisterClientScriptInclude("SyrinxAjax", scriptUrl);
			}

			base.OnLoad(e);
			//if (Request.UserAgent.IndexOf("AppleWebKit") > 0)
			//Request.Browser.Adapters.Clear();
			//Request.Browser.Adapters.Remove(typeof(Menu).AssemblyQualifiedName);
		}

		public override string processUrl(string url)
		{
			url = base.processUrl(url);

			if (m_checkForSslLinks && url.IndexOf(':') == -1)
			{
				bool isSecure = false;
				Swaf.IResults rc = App.ruleMgr.execute("isSecurePage", url);
				if (rc.CallResults is bool)
					isSecure = (bool)rc.CallResults;
				if (isSecure && m_hostUrl != null)
					url = "https://" + m_hostUrl + url;
				else if (!isSecure && m_inSslNow)
				{
					Uri first = (Uri)App.sessionData.get("firstUrl");
					string backUrl = "http://" + first.Host;
					if (!first.IsDefaultPort)
						backUrl += ":" + first.Port;
					url = backUrl + url;
				}
			}


			return url;
		}

		protected override void OnPreRender(EventArgs e)
		{
			//The following is Hack-ish type code to help preload known script libraries into the browser.  Without this,
			//if an nitobibizobjdisplay changes from a template that didnt have one of the below control to one that did,
			//The control would not initialize correctly because its base library wasnt available.  This ensures they are
			//available.
			if (m_usingAjax)
			{
				Nitobi.NitobiControlBase.SetupScripts(this);
				Combo.SetupScriptsAndStyleSheets(this, Nitobi.ComboThemeName.Tiger);
			}
            CkEditor.setupScripts(this);
			base.OnPreRender(e);

			if (m_usingAjax) 
			{
				StringBuilder buff = new StringBuilder(1024);
				buff.AppendFormat("var bizProcManager = new BizProcCoordinator('{0}', '{1}');\n", ViewName, Page.Response.ApplyAppPathModifier(m_ajaxHandler));
				foreach (ActiveBizProc bizproc in BPC.ActiveBizProcs.Values)
					buff.AppendFormat("var {0}BizProc = new ClientBizProc('{0}');\n", bizproc.BizProc.Id);
				ClientScript.RegisterClientScriptBlock(typeof(NitobiBasePage), "BizProcCoord", buff.ToString(), true);
			}
		}
		public bool UsingAjax { get { return m_usingAjax; } }
		public bool IsAjaxEvent { get { return false; } }
	}
}
